/**
 * Formats a date string or Date object.
 * @param {string | Date} dateInput - The date to format.
 * @param {string} format - The desired format string (e.g., 'YYYY-MM-DD HH:mm:ss', 'YYYY-MM-DD').
 * @returns {string} The formatted date string, or an empty string if input is invalid.
 */
export function formatDate(dateInput, format = 'YYYY-MM-DD HH:mm:ss') {
  if (!dateInput) return '';

  let date;
  if (typeof dateInput === 'string') {
    date = new Date(dateInput.replace(/-/g, '/')); // Replace hyphens for better cross-browser compatibility with new Date()
  } else if (dateInput instanceof Date) {
    date = dateInput;
  } else {
    return ''; // Invalid input type
  }

  if (isNaN(date.getTime())) {
    return ''; // Invalid date
  }

  const year = date.getFullYear();
  const month = (date.getMonth() + 1).toString().padStart(2, '0');
  const day = date.getDate().toString().padStart(2, '0');
  const hours = date.getHours().toString().padStart(2, '0');
  const minutes = date.getMinutes().toString().padStart(2, '0');
  const seconds = date.getSeconds().toString().padStart(2, '0');

  let formattedDate = format;
  formattedDate = formattedDate.replace('YYYY', year.toString());
  formattedDate = formattedDate.replace('MM', month);
  formattedDate = formattedDate.replace('DD', day);
  formattedDate = formattedDate.replace('HH', hours);
  formattedDate = formattedDate.replace('mm', minutes);
  formattedDate = formattedDate.replace('ss', seconds);

  return formattedDate;
}

// You can add other formatting utility functions here if needed. 